import json
import os
from config.config import Config
from pydantic import BaseModel, Field


class LeafNodeInfo(BaseModel):
    math_widget: str = Field(default="")
    info_image_widget: str = Field(default="")
    params: list = Field(default_factory=list)
    label_text: str = Field(default="")
    math_label_text: str = Field(default="")


def get_node_info(node_text):
    file_dir = os.path.join(Config.leaf_info_path, node_text.toPlainText() + '.json')
    if not os.path.exists(file_dir):
        file_dir = os.path.join(Config.leaf_info_path, 'default.json')
    return LeafNodeInfo.parse_file(file_dir)


def save_node_info(node_text, leaf_info):
    file_dir = os.path.join(Config.leaf_info_path, node_text.toPlainText() + '.json')
    with open(file_dir, 'w') as f:
        json.dump(leaf_info.dict(), f)
